package com.zz.reflect;

import org.junit.jupiter.api.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class TestCar {

    // 1 获取class 的多种方式
    @Test
    public void test01() throws Exception {
        //  1  类名.class
        Class clazz1 = Car.class;
        //  2  对象.getclass
        Class clazz2 = new Car().getClass();
        //  class.forname（全路径）
        Class<?> clazz3 = Class.forName("com.zz.reflect.Car");


        //  实例化
        Car car = (Car) clazz3.getDeclaredConstructor().newInstance();
        System.out.println(car);

    }


//    2  获取构造方法
    @Test
    public void test02() throws Exception {
        Class<Car> clazz = Car.class;
        // 获取所有的构造  getConstructors 获取所有 public
 //       Constructor[] constructors = clazz.getConstructors();
        // getDeclaredConstructors 获取所有 public
        Constructor[] constructors = clazz.getDeclaredConstructors();
        for (Constructor c : constructors) {
            System.out.println(c.getName()+".."+c.getParameterCount()+".."+c.getAnnotatedReturnType());
        }

        //  指定有参数的构造创建对象

        // public
//        Constructor<Car> c1 = clazz.getConstructor(String.class, int.class, String.class);
//        Car car1 = c1.newInstance("夏利", 10, "红色");
//        System.out.println(car1);

        //private
        Constructor<Car> c2 = clazz.getDeclaredConstructor(String.class, int.class, String.class);
        c2.setAccessible(true);
        Car car = c2.newInstance("夏利", 10, "红色");
        System.out.println(car);

    }

//    3  获取属性
    @Test
    public void test03()throws Exception{
        //  public
        Class<Car> clazz = Car.class;
        Car car = clazz.getDeclaredConstructor().newInstance();
//        Field[] fields = clazz.getDeclaredFields();
        // 所有的 private
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("name")){
                field.setAccessible(true);
                field.set(car,"五菱宏光");
            }
            System.out.println(field.getName());
            System.out.println(car);
        }


    }
    //    4  获取方法  包括私有的
    @Test
    public void test04()throws Exception{
        Car car = new Car("奔驰",10,"黑色");
        Class clazz = car.getClass();
        //  public
        Method[] methods = clazz.getMethods();
        for (Method m1 : methods) {
           // System.out.println(m1.getName());
            //
            if (m1.getName().equals("toString")){
                String invoke = (String) m1.invoke(car);
                System.out.println("toString"+invoke);
            }
        }
        //  private
        Method[] declaredMethodsAll = clazz.getDeclaredMethods();
        for (Method m2 : declaredMethodsAll) {
            if (m2.getName().equals("run")){
                m2.setAccessible(true);
                m2.invoke(car);
            }

        }

    }

}
